<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
    <title>Coverage Report :: MapParser</title>
    <style type="text/css">
        @import "../../.css/coverage.css";
    </style>
</head>

<body>
<div class="header"></div>

<div class="content">
    <div class="breadCrumbs">
        [ <a href="../../index.html">all classes</a> ]
        [ <a href="../index.html">nl.tudelft.jpacman.level</a> ]
    </div>

    <h1>Coverage Summary for Class: MapParser (nl.tudelft.jpacman.level)</h1>

    <table class="coverageStats">
        <tr>
            <th class="name">Class</th>
            <th class="coverageStat
">
                Class, %
            </th>
            <th class="coverageStat
">
                Method, %
            </th>
            <th class="coverageStat
">
                Line, %
            </th>
        </tr>
        <tr>
            <td class="name">MapParser</td>
            <td class="coverageStat">
  <span class="percent">
    100%
  </span>
                <span class="absValue">
    (1/ 1)
  </span>
            </td>
            <td class="coverageStat">
  <span class="percent">
    90%
  </span>
                <span class="absValue">
    (9/ 10)
  </span>
            </td>
            <td class="coverageStat">
  <span class="percent">
    91.5%
  </span>
                <span class="absValue">
    (65/ 71)
  </span>
            </td>
        </tr>

    </table>

    <br/>
    <br/>


    <div class="sourceCode"><i>1</i>&nbsp;package nl.tudelft.jpacman.level;
        <i>2</i>&nbsp;
        <i>3</i>&nbsp;import java.io.BufferedReader;
        <i>4</i>&nbsp;import java.io.IOException;
        <i>5</i>&nbsp;import java.io.InputStream;
        <i>6</i>&nbsp;import java.io.InputStreamReader;
        <i>7</i>&nbsp;import java.util.ArrayList;
        <i>8</i>&nbsp;import java.util.List;
        <i>9</i>&nbsp;
        <i>10</i>&nbsp;import nl.tudelft.jpacman.PacmanConfigurationException;
        <i>11</i>&nbsp;import nl.tudelft.jpacman.board.Board;
        <i>12</i>&nbsp;import nl.tudelft.jpacman.board.BoardFactory;
        <i>13</i>&nbsp;import nl.tudelft.jpacman.board.Square;
        <i>14</i>&nbsp;import nl.tudelft.jpacman.npc.Ghost;
        <i>15</i>&nbsp;import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
        <i>16</i>&nbsp;
        <i>17</i>&nbsp;/**
        <i>18</i>&nbsp; * Creates new {@link Level}s from text representations.
        <i>19</i>&nbsp; *
        <i>20</i>&nbsp; * @author Jeroen Roosen
        <i>21</i>&nbsp; */
        <i>22</i>&nbsp;public class MapParser {
        <i>23</i>&nbsp;
        <i>24</i>&nbsp; /**
        <i>25</i>&nbsp; * The factory that creates the levels.
        <i>26</i>&nbsp; */
        <i>27</i>&nbsp; private final LevelFactory levelCreator;
        <i>28</i>&nbsp;
        <i>29</i>&nbsp; /**
        <i>30</i>&nbsp; * The factory that creates the squares and board.
        <i>31</i>&nbsp; */
        <i>32</i>&nbsp; private final BoardFactory boardCreator;
        <i>33</i>&nbsp;
        <i>34</i>&nbsp; /**
        <i>35</i>&nbsp; * Creates a new map parser.
        <i>36</i>&nbsp; *
        <i>37</i>&nbsp; * @param levelFactory The factory providing the NPC objects and the level.
        <i>38</i>&nbsp; * @param boardFactory The factory providing the Square objects and the board.
        <i>39</i>&nbsp; */
        <b class="fc"><i>40</i>&nbsp; public MapParser(LevelFactory levelFactory, BoardFactory boardFactory) {</b>
        <b class="fc"><i>41</i>&nbsp; this.levelCreator = levelFactory;</b>
        <b class="fc"><i>42</i>&nbsp; this.boardCreator = boardFactory;</b>
        <b class="fc"><i>43</i>&nbsp; }</b>
        <i>44</i>&nbsp;
        <i>45</i>&nbsp; /**
        <i>46</i>&nbsp; * Parses the text representation of the board into an actual level.
        <i>47</i>&nbsp; *
        <i>48</i>&nbsp; * &lt;ul&gt;
        <i>49</i>&nbsp; * &lt;li&gt;Supported characters:
        <i>50</i>&nbsp; * &lt;li&gt;&#39; &#39; (space) an empty square.
        <i>51</i>&nbsp; * &lt;li&gt;&#39;#&#39; (bracket) a wall.
        <i>52</i>&nbsp; * &lt;li&gt;&#39;.&#39; (period) a square with a pellet.
        <i>53</i>&nbsp; * &lt;li&gt;&#39;P&#39; (capital P) a starting square for players.
        <i>54</i>&nbsp; * &lt;li&gt;&#39;G&#39; (capital G) a square with a ghost.
        <i>55</i>&nbsp; * &lt;/ul&gt;
        <i>56</i>&nbsp; *
        <i>57</i>&nbsp; * @param map The text representation of the board, with map[x][y]
        <i>58</i>&nbsp; * representing the square at position x,y.
        <i>59</i>&nbsp; * @return The level as represented by this text.
        <i>60</i>&nbsp; */
        <i>61</i>&nbsp; public Level parseMap(char[][] map) {
        <b class="fc"><i>62</i>&nbsp; int width = map.length;</b>
        <b class="fc"><i>63</i>&nbsp; int height = map[0].length;</b>
        <i>64</i>&nbsp;
        <b class="fc"><i>65</i>&nbsp; Square[][] grid = new Square[width][height];</b>
        <i>66</i>&nbsp;
        <b class="fc"><i>67</i>&nbsp; List&lt;Ghost&gt; ghosts = new ArrayList&lt;&gt;();</b>
        <b class="fc"><i>68</i>&nbsp; List&lt;Square&gt; startPositions = new ArrayList&lt;&gt;();</b>
        <i>69</i>&nbsp;
        <b class="fc"><i>70</i>&nbsp; makeGrid(map, width, height, grid, ghosts, startPositions);</b>
        <i>71</i>&nbsp;
        <b class="fc"><i>72</i>&nbsp; Board board = boardCreator.createBoard(grid);</b>
        <b class="fc"><i>73</i>&nbsp; return levelCreator.createLevel(board, ghosts, startPositions);</b>
        <i>74</i>&nbsp; }
        <i>75</i>&nbsp;
        <i>76</i>&nbsp; private void makeGrid(char[][] map, int width, int height,
        <i>77</i>&nbsp; Square[][] grid, List&lt;Ghost&gt; ghosts, List&lt;Square&gt; startPositions) {
        <b class="fc"><i>78</i>&nbsp; for (int x = 0; x &lt; width; x++) {</b>
        <b class="fc"><i>79</i>&nbsp; for (int y = 0; y &lt; height; y++) {</b>
        <b class="fc"><i>80</i>&nbsp; char c = map[x][y];</b>
        <b class="fc"><i>81</i>&nbsp; addSquare(grid, ghosts, startPositions, x, y, c);</b>
        <i>82</i>&nbsp; }
        <i>83</i>&nbsp; }
        <b class="fc"><i>84</i>&nbsp; }</b>
        <i>85</i>&nbsp;
        <i>86</i>&nbsp; /**
        <i>87</i>&nbsp; * Adds a square to the grid based on a given character. These
        <i>88</i>&nbsp; * character come from the map files and describe the type
        <i>89</i>&nbsp; * of square.
        <i>90</i>&nbsp; *
        <i>91</i>&nbsp; * @param grid The grid of squares with board[x][y] being the
        <i>92</i>&nbsp; * square at column x, row y.
        <i>93</i>&nbsp; * @param ghosts List of all ghosts that were added to the map.
        <i>94</i>&nbsp; * @param startPositions List of all start positions that were added
        <i>95</i>&nbsp; * to the map.
        <i>96</i>&nbsp; * @param x x coordinate of the square.
        <i>97</i>&nbsp; * @param y y coordinate of the square.
        <i>98</i>&nbsp; * @param c Character describing the square type.
        <i>99</i>&nbsp; */
        <i>100</i>&nbsp; protected void addSquare(Square[][] grid, List&lt;Ghost&gt; ghosts,
        <i>101</i>&nbsp; List&lt;Square&gt; startPositions, int x, int y, char c) {
        <b class="fc"><i>102</i>&nbsp; switch (c) {</b>
        <i>103</i>&nbsp; case &#39; &#39;:
        <b class="fc"><i>104</i>&nbsp; grid[x][y] = boardCreator.createGround();</b>
        <b class="fc"><i>105</i>&nbsp; break;</b>
        <i>106</i>&nbsp; case &#39;#&#39;:
        <b class="fc"><i>107</i>&nbsp; grid[x][y] = boardCreator.createWall();</b>
        <b class="fc"><i>108</i>&nbsp; break;</b>
        <i>109</i>&nbsp; case &#39;.&#39;:
        <b class="fc"><i>110</i>&nbsp; Square pelletSquare = boardCreator.createGround();</b>
        <b class="fc"><i>111</i>&nbsp; grid[x][y] = pelletSquare;</b>
        <b class="fc"><i>112</i>&nbsp; levelCreator.createPellet().occupy(pelletSquare);</b>
        <b class="fc"><i>113</i>&nbsp; break;</b>
        <i>114</i>&nbsp; case &#39;G&#39;:
        <b class="fc"><i>115</i>&nbsp; Square ghostSquare = makeGhostSquare(ghosts, levelCreator.createGhost());</b>
        <b class="fc"><i>116</i>&nbsp; grid[x][y] = ghostSquare;</b>
        <b class="fc"><i>117</i>&nbsp; break;</b>
        <i>118</i>&nbsp; case &#39;P&#39;:
        <b class="fc"><i>119</i>&nbsp; Square playerSquare = boardCreator.createGround();</b>
        <b class="fc"><i>120</i>&nbsp; grid[x][y] = playerSquare;</b>
        <b class="fc"><i>121</i>&nbsp; startPositions.add(playerSquare);</b>
        <b class="fc"><i>122</i>&nbsp; break;</b>
        <i>123</i>&nbsp; default:
        <b class="nc"><i>124</i>&nbsp; throw new PacmanConfigurationException(&quot;Invalid character at &quot;</b>
        <i>125</i>&nbsp; + x + &quot;,&quot; + y + &quot;: &quot; + c);
        <i>126</i>&nbsp; }
        <b class="fc"><i>127</i>&nbsp; }</b>
        <i>128</i>&nbsp;
        <i>129</i>&nbsp; /**
        <i>130</i>&nbsp; * creates a Square with the specified ghost on it
        <i>131</i>&nbsp; * and appends the placed ghost into the ghost list.
        <i>132</i>&nbsp; *
        <i>133</i>&nbsp; * @param ghosts all the ghosts in the level so far, the new ghost will be appended
        <i>134</i>&nbsp; * @param ghost the newly created ghost to be placed
        <i>135</i>&nbsp; * @return a square with the ghost on it.
        <i>136</i>&nbsp; */
        <i>137</i>&nbsp; protected Square makeGhostSquare(List&lt;Ghost&gt; ghosts, Ghost ghost) {
        <b class="fc"><i>138</i>&nbsp; Square ghostSquare = boardCreator.createGround();</b>
        <b class="fc"><i>139</i>&nbsp; ghosts.add(ghost);</b>
        <b class="fc"><i>140</i>&nbsp; ghost.occupy(ghostSquare);</b>
        <b class="fc"><i>141</i>&nbsp; return ghostSquare;</b>
        <i>142</i>&nbsp; }
        <i>143</i>&nbsp;
        <i>144</i>&nbsp; /**
        <i>145</i>&nbsp; * Parses the list of strings into a 2-dimensional character array and
        <i>146</i>&nbsp; * passes it on to {@link #parseMap(char[][])}.
        <i>147</i>&nbsp; *
        <i>148</i>&nbsp; * @param text The plain text, with every entry in the list being a equally
        <i>149</i>&nbsp; * sized row of squares on the board and the first element being
        <i>150</i>&nbsp; * the top row.
        <i>151</i>&nbsp; * @return The level as represented by the text.
        <i>152</i>&nbsp; * @throws PacmanConfigurationException If text lines are not properly formatted.
        <i>153</i>&nbsp; */
        <i>154</i>&nbsp; public Level parseMap(List&lt;String&gt; text) {
        <i>155</i>&nbsp;
        <b class="fc"><i>156</i>&nbsp; checkMapFormat(text);</b>
        <i>157</i>&nbsp;
        <b class="fc"><i>158</i>&nbsp; int height = text.size();</b>
        <b class="fc"><i>159</i>&nbsp; int width = text.get(0).length();</b>
        <i>160</i>&nbsp;
        <b class="fc"><i>161</i>&nbsp; char[][] map = new char[width][height];</b>
        <b class="fc"><i>162</i>&nbsp; for (int x = 0; x &lt; width; x++) {</b>
        <b class="fc"><i>163</i>&nbsp; for (int y = 0; y &lt; height; y++) {</b>
        <b class="fc"><i>164</i>&nbsp; map[x][y] = text.get(y).charAt(x);</b>
        <i>165</i>&nbsp; }
        <i>166</i>&nbsp; }
        <b class="fc"><i>167</i>&nbsp; return parseMap(map);</b>
        <i>168</i>&nbsp; }
        <i>169</i>&nbsp;
        <i>170</i>&nbsp; /**
        <i>171</i>&nbsp; * Check the correctness of the map lines in the text.
        <i>172</i>&nbsp; *
        <i>173</i>&nbsp; * @param text Map to be checked
        <i>174</i>&nbsp; * @throws PacmanConfigurationException if map is not OK.
        <i>175</i>&nbsp; */
        <i>176</i>&nbsp; private void checkMapFormat(List&lt;String&gt; text) {
        <b class="fc"><i>177</i>&nbsp; if (text == null) {</b>
        <b class="nc"><i>178</i>&nbsp; throw new PacmanConfigurationException(</b>
        <i>179</i>&nbsp; &quot;Input text cannot be null.&quot;);
        <i>180</i>&nbsp; }
        <i>181</i>&nbsp;
        <b class="fc"><i>182</i>&nbsp; if (text.isEmpty()) {</b>
        <b class="nc"><i>183</i>&nbsp; throw new PacmanConfigurationException(</b>
        <i>184</i>&nbsp; &quot;Input text must consist of at least 1 row.&quot;);
        <i>185</i>&nbsp; }
        <i>186</i>&nbsp;
        <b class="fc"><i>187</i>&nbsp; int width = text.get(0).length();</b>
        <i>188</i>&nbsp;
        <b class="fc"><i>189</i>&nbsp; if (width == 0) {</b>
        <b class="nc"><i>190</i>&nbsp; throw new PacmanConfigurationException(</b>
        <i>191</i>&nbsp; &quot;Input text lines cannot be empty.&quot;);
        <i>192</i>&nbsp; }
        <i>193</i>&nbsp;
        <b class="fc"><i>194</i>&nbsp; for (String line : text) {</b>
        <b class="fc"><i>195</i>&nbsp; if (line.length() != width) {</b>
        <b class="nc"><i>196</i>&nbsp; throw new PacmanConfigurationException(</b>
        <i>197</i>&nbsp; &quot;Input text lines are not of equal width.&quot;);
        <i>198</i>&nbsp; }
        <b class="fc"><i>199</i>&nbsp; }</b>
        <b class="fc"><i>200</i>&nbsp; }</b>
        <i>201</i>&nbsp;
        <i>202</i>&nbsp; /**
        <i>203</i>&nbsp; * Parses the provided input stream as a character stream and passes it
        <i>204</i>&nbsp; * result to {@link #parseMap(List)}.
        <i>205</i>&nbsp; *
        <i>206</i>&nbsp; * @param source The input stream that will be read.
        <i>207</i>&nbsp; * @return The parsed level as represented by the text on the input stream.
        <i>208</i>&nbsp; * @throws IOException when the source could not be read.
        <i>209</i>&nbsp; */
        <i>210</i>&nbsp; public Level parseMap(InputStream source) throws IOException {
        <b class="fc"><i>211</i>&nbsp; try (BufferedReader reader = new BufferedReader(new InputStreamReader(</b>
        <i>212</i>&nbsp; source, &quot;UTF-8&quot;))) {
        <b class="fc"><i>213</i>&nbsp; List&lt;String&gt; lines = new ArrayList&lt;&gt;();</b>
        <b class="fc"><i>214</i>&nbsp; while (reader.ready()) {</b>
        <b class="fc"><i>215</i>&nbsp; lines.add(reader.readLine());</b>
        <i>216</i>&nbsp; }
        <b class="fc"><i>217</i>&nbsp; return parseMap(lines);</b>
        <b class="fc"><i>218</i>&nbsp; }</b>
        <i>219</i>&nbsp; }
        <i>220</i>&nbsp;
        <i>221</i>&nbsp; /**
        <i>222</i>&nbsp; * Parses the provided input stream as a character stream and passes it
        <i>223</i>&nbsp; * result to {@link #parseMap(List)}.
        <i>224</i>&nbsp; *
        <i>225</i>&nbsp; * @param mapName Name of a resource that will be read.
        <i>226</i>&nbsp; * @return The parsed level as represented by the text on the input stream.
        <i>227</i>&nbsp; * @throws IOException when the resource could not be read.
        <i>228</i>&nbsp; */
        <i>229</i>&nbsp; @SuppressFBWarnings(
        <i>230</i>&nbsp; value = {&quot;OBL_UNSATISFIED_OBLIGATION&quot;, &quot;RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE&quot;},
        <i>231</i>&nbsp; justification = &quot;try with resources always cleans up / false positive in java 11&quot;
        <i>232</i>&nbsp; )
        <i>233</i>&nbsp; public Level parseMap(String mapName) throws IOException {
        <b class="fc"><i>234</i>&nbsp; try (InputStream boardStream = MapParser.class.getResourceAsStream(mapName))
            {</b>
        <b class="fc"><i>235</i>&nbsp; if (boardStream == null) {</b>
        <b class="fc"><i>236</i>&nbsp; throw new PacmanConfigurationException(&quot;Could not get resource for: &quot; +
            mapName);</b>
        <i>237</i>&nbsp; }
        <b class="fc"><i>238</i>&nbsp; return parseMap(boardStream);</b>
        <b class="fc"><i>239</i>&nbsp; }</b>
        <i>240</i>&nbsp; }
        <i>241</i>&nbsp;
        <i>242</i>&nbsp; /**
        <i>243</i>&nbsp; * @return the BoardCreator
        <i>244</i>&nbsp; */
        <i>245</i>&nbsp; protected BoardFactory getBoardCreator() {
        <b class="nc"><i>246</i>&nbsp; return boardCreator;</b>
        <i>247</i>&nbsp; }
        <i>248</i>&nbsp;
        <i>249</i>&nbsp;}
    </div>
</div>

<div class="footer">

    <div style="float:right;">generated on 2023-04-25 16:45</div>
</div>
</body>
</html>
